/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bground.network.server;

import bground.collisions.BulletFieldCollision;
import bground.collisions.PlayerBulletCollision;
import bground.collisions.PlayerFieldCollision;
import bground.game.GameField;
import bground.sprites.Border;
import bground.sprites.Enemy;
import bground.sprites.Tree;
import external.golden.gamedev.object.Background;
import external.golden.gamedev.object.PlayField;
import external.golden.gamedev.object.Sprite;
import external.golden.gamedev.object.SpriteGroup;
import external.golden.gamedev.object.background.ColorBackground;
import java.awt.Color;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author s6_kustan_e
 */
public class GameLoop implements Runnable, GameField {

    boolean isRunning;
    PlayField playfield;
    Background background;
    SpriteGroup players;
    SpriteGroup bullets;
    SpriteGroup field;
    GameServer server;
    ArrayList<Command> commands;
    static final int delay = 100; // 1000 / turns

    public void addPlayer(Sprite s) {
        players.add(s);
    }

    public void addBullet(Sprite s) {
        bullets.add(s);
    }

    public void addField(Sprite s) {
        field.add(s);
    }

    public GameLoop() throws IOException {
        commands = new ArrayList<Command>();
        server = new GameServer(5324);
        Thread runner = new Thread(this);
        playfield = new PlayField();
        background = new ColorBackground(Color.YELLOW, 5000, 5000);

        players = new SpriteGroup("Players");
        bullets = new SpriteGroup("Bullets");
        field = new SpriteGroup("Field");

        playfield.setBackground(background);
        Enemy e;

        for (int i = 0; i < 10; i++) {
            e = new Enemy(this);
            e.setBackground(background);
            addPlayer(e);
        }
        for (int i = 250; i < 1000; i += 100) {
            for (int j = 250; j < 1000; j += 100) {
                Tree tree = new Tree();
                tree.setLocation(i, j);
                tree.setBackground(background);
                addField(tree);
            }

        }

        for (int i = 0; i <= 4950; i += 50) {
            for (int j = 0; j <= 4950; j += 50) {
                if ((i % 4950) * (j % 4950) == 0) {
                    Border b = new Border();
                    b.setLocation(i, j);
                    b.setBackground(background);
                    addField(b);
                }
            }

        }

        playfield.addGroup(players);
        playfield.addGroup(bullets);
        playfield.addGroup(field);

        playfield.addCollisionGroup(players, bullets, new PlayerBulletCollision());
        playfield.addCollisionGroup(players, field, new PlayerFieldCollision());
        playfield.addCollisionGroup(bullets, field, new BulletFieldCollision());

        turn = 0;
        isRunning = true;
        runner.start();
        last_update = System.currentTimeMillis();
        start_time = System.currentTimeMillis();
        new Thread(new Runnable() {

            long time;
            long nexttime;

            public void run() {
                time = System.currentTimeMillis();
                while (true) {
                    nexttime = System.currentTimeMillis() - time;
                    playfield.update(nexttime);
                    server.update(nexttime);
                    time += nexttime;
                }
            }
        });
    }
    int turn;
    long last_update;
    long start_time;

    public void run() {
        while (isRunning) {
            turn++;
            long update = System.currentTimeMillis() - last_update;
            last_update = System.currentTimeMillis();

            server.getCommands();

            //Processing events in queue
            if (turn % 10 == 0) { //sync turn
            } else { //normal turn
            }
            //EventHandling

            long
             ctime = System.currentTimeMillis();
            if (start_time + (turn * delay) > ctime) {
                try {
                    Thread.sleep(start_time + (turn * delay) - ctime);
                } catch (InterruptedException ex) {
                }
            }


        }

    }
    public static void main(String [] args) {
        try {
            new GameLoop();
        } catch (IOException ex) {
            Logger.getLogger(GameLoop.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
